/**
 * 在这里定义和用户相关的路由处理函数，供 /router/user.js 模块进行调用
 */
//导入数据库操作模块
var models = require('../db/index');
var mysql = require('mysql');

// 连接数据库
var conn = mysql.createConnection(models.mysql);
conn.connect();

//新增班级
exports.addClass = (req, res) => {
    /* 检测表单数据是否合法 */
    // 接收表单数据
    var body = req.body;
    console.log(23, body);
    // 插入新班级到数据库
    const sql = `insert into classman set ?`;
    conn.query(sql, body, function (err, results) {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // SQL 语句执行成功，但影响行数不为 1
        if (results.affectedRows !== 1) {
            return res.cc('新增班级失败，请稍后再试！');
        } else {
            return res.send({
                status: 0,
                message: '新增班级成功',
            })
        }
    })
}

//获取班级信息
exports.getClass = (req, res) => {
    var info = req.query;
    const sql = `select * from classman`;
    conn.query(sql, info, (err, results) => {
        if (err) return res.cc(err);
        res.send({
            status: 200,
            data: results,
        })
    })
}

//修改班级信息
exports.editClass = (req, res) => {
    var info = req.body;
    var body = req.params;
    const sql = `update classman set classname='${info.classname}',classroom='${info.classroom}',manger='${info.manger}',
    teacherman='${info.teacherman}',job='${info.job}',startime='${info.startime}',endtime='${info.endtime}' where classid='${body.id}'`;
    conn.query(sql, (err, results) => {
        if (err) return res.cc(err);
        res.send({
            status: 200,
            message: '修改成功'
        })
    })
}

// 删除班级
exports.deleteClass = (req, res) => {
    var query = req.query;
    const sql = `delete from classman where classid = ?;`;
    conn.query(sql, query.id, (err, results) => {
        if (err) return res.cc(err);
        res.send({
            status: 200,
            message: '删除成功'
        })
    })
}

// 模糊查询班级名字
exports.obscureClass = (req, res) => {
    var body = req.body;
    const sql = `select * from classman where classname like '%${body.classname}%';`;
    conn.query(sql, (err, results) => {
        if (err) return res.cc(err);
        res.send({
            status: 200,
            message: '查询成功',
            data: results
        })
    })
}